import requests 
import json  

# 测试导出excel文件并保存到本地

def main1():

    url1="http://heb.hsip.gov.cn/ims-hb//web/data/query/exportHilistCode"
    token1='946eceb1-3eb0-43f4-be8d-676c070f0b5c'
    session1='NDBmYTAxZDMtNzllMy00YTU0LWJhYzItYTI2MGUxOTk0ZjMx'
    savePath='d:/hlsp1.xlsx'

    data1={"clrOptins":"130534","fixmedinsCode":"H13053400007","begndate":"2024-01-01",
           "enddate":"2024-05-16",  "groupVal":"1"}
    headers1={"Accept":"application/json, text/plain, */*",
              "Accept-Language":"zh-CN,zh;q=0.9",
              'Connection':'keep-alive',
              'Content-Type':'application/json; charset=UTF-8',
              "Cookie":f"XSRF-TOKEN={token1}; SESSION={session1}",
              "Origin":"http://heb.hsip.gov.cn",
              "Referer":"http://heb.hsip.gov.cn/ims-hb/ui/W0104.html",
              "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
              "X-XSRF-TOKEN":f"{token1}"
              }
    
    print(data1)

    resp1=requests.post(url=url1,json=data1,headers=headers1,verify=False,stream=True)
    if resp1.status_code!=200 :
        print('请求失败，状态为 '+resp1.status_code)
        return
    
    with open(savePath,'wb') as f1:
        for chunk in resp1.iter_content():
            f1.write(chunk)

    # 结果见附件 hsip1.xlsx
    

if __name__ == '__main__':  
    main1()

